package com.webtest.mvctest1;

import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/7/26 10:39
 */
public class StudentDao {
    public List<Student> getAllStudentList() {
        List<Student> students = new ArrayList<>();

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultset = null;
        try {
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("appConfig.properties");
            Properties properties = new Properties();
            properties.load(in);
            String driver = properties.getProperty("db.driver");
            String url = properties.getProperty("db.url");
            String username = properties.getProperty("db.username");
            String password = properties.getProperty("db.password");

            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "SELECT flowid,type,idcard,examcard,studentname,location,grade FROM student";
            preparedStatement = connection.prepareStatement(sql);
            resultset = preparedStatement.executeQuery();
            while (resultset.next()) {
                Student student = new Student();
                student.setFlowId(resultset.getInt(1));
                student.setType(resultset.getInt(2));
                student.setIdCard(resultset.getString(3));
                student.setExamCard(resultset.getString(4));
                student.setStudentName(resultset.getString(5));
                student.setLocation(resultset.getString(6));
                student.setGrade(resultset.getInt(7));
                students.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (resultset != null) {
                try {
                    resultset.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return students;
    }

    public boolean delStudent(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("appConfig.properties");
            Properties properties = new Properties();
            properties.load(in);
            String driver = properties.getProperty("db.driver");
            String url = properties.getProperty("db.url");
            String username = properties.getProperty("db.username");
            String password = properties.getProperty("db.password");

            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
            String sql = "DELETE FROM student WHERE flowId=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            result = preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return result > 0;
    }
}
